package com.gitee.liuzhi.design.create;

/**
 * @Author liuzhi
 * @Date 2023/8/26 19:37
 * @Version 1.0
 * @Description 抽象工厂模式 Abstract Factory Pattern
 * <p>
 * 抽象工厂模式又称为工厂的工厂
 * 在抽象工厂模式中, 接口是负责创建一个相关对象的工厂, 不需要显式指定它们的类, 每个生成的工厂都能按照工厂模式提供对象
 */
public class AbstractFactoryPattern {

    public static void main(String[] args) {
        /*
        使用 FactoryProducer 来获取 AbstractFactory, 通过传递类型信息来获取实体类的对象
         */
        AbstractFactory brand = FactoryProducer.getFactory("brand");
        assert brand != null;
        Brand benz = brand.getBrand("benz");
        benz.getBrand();
        AbstractFactory color = FactoryProducer.getFactory("color");
        assert color != null;
        Color red = color.getColor("red");
        red.getColor();
    }

    /**
     * 创建一个工厂创造器/生成器, 通过传递品牌和颜色来获取工厂
     */
    public static class FactoryProducer {
        public static AbstractFactory getFactory(String factoryName){
            if ("brand".equals(factoryName)) {
                return new BrandFactory();
            } else if ("color".equals(factoryName)) {
                return new ColorFactory();
            }
            return null;
        }
    }

    /**
     * 扩展 AbstractFactory 的工厂类, 基于给定的信息生成实体类对象
     */
    public static class ColorFactory implements AbstractFactory {
        @Override
        public Color getColor(String colorName) {
            if("red".equals(colorName)){
                return new Red();
            } else if ("Black".equals(colorName)){
                return new Black();
            } else if ("green".equals(colorName)){
                return new Green();
            }
            return null;
        }

        @Override
        public Brand getBrand(String brandName) {
            return null;
        }
    }

    public static class BrandFactory implements AbstractFactory{
        @Override
        public Color getColor(String colorName) {
            return null;
        }

        @Override
        public Brand getBrand(String brandName) {
            if("audi".equals(brandName)){
                return new Audi();
            } else if ("benz".equals(brandName)){
                return new Benz();
            } else if ("bmw".equals(brandName)){
                return new BMW();
            }
            return null;
        }
    }

    /**
     * 为品牌和颜色对象创建抽象类来获取工厂
     */
    public interface AbstractFactory {
        Color getColor(String colorName);
        Brand getBrand(String brandName);
    }


    /**
     * 品牌接口
     */
    public interface Brand {
        void getBrand();
    }
    public static class Audi implements Brand {
        @Override
        public void getBrand() {
            System.out.println("品牌：奥迪");
        }
    }
    public static class Benz implements Brand {
        @Override
        public void getBrand() {
            System.out.println("品牌：奔驰");
        }
    }
    public static class BMW implements Brand {
        @Override
        public void getBrand() {
            System.out.println("品牌：宝马");
        }
    }

    /**
     * 颜色接口
     */
    public interface Color {
        void getColor();
    }
    public static class Red implements Color {
        @Override
        public void getColor() {
            System.out.println("颜色：红色");
        }
    }
    public static class Black implements Color {
        @Override
        public void getColor() {
            System.out.println("颜色：黑色");
        }
    }
    public static class Green implements Color {
        @Override
        public void getColor() {
            System.out.println("颜色：绿色");
        }
    }

}
